package com.huike.web.controller.review;


import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.review.pojo.PageBean;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**=========================================================新增数据============================================*/
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult add1(@PathVariable String name, @PathVariable Integer age , @PathVariable String sex){
        int i = reviewService.add1(name, age, sex);
        if (i>0){
            return AjaxResult.success("成功插入:1条数据",200);
        }else {
            return AjaxResult.error();
        }
    }



    @GetMapping("/saveData")
    public AjaxResult add2(@RequestBody Review review){
        int i = reviewService.add2(review);
        if (i>0){
            return AjaxResult.success("成功插入:1条数据",200);
        }else {
            return AjaxResult.error();
        }
    }
    /**=========================================================删除数据=============================================*/
    @DeleteMapping("/remove/{id}")
    public AjaxResult deleteById(@PathVariable Long id){
        int i = reviewService.deleteById(id);
        if (i>0){
            return AjaxResult.success("成功删除:1条数据",200);
        }else {
            return AjaxResult.error();
        }
    }


    /**=========================================================修改数据=============================================*/

    @PostMapping("/update")
    public AjaxResult update(@RequestBody Review review){
        int i = reviewService.updateById(review);
        if (i>0){
            return AjaxResult.success("修改成功",200);
        }else {
            return AjaxResult.error();
        }
    }


    /**=========================================================查询数据=============================================*/
    @GetMapping("/getById")
    public AjaxResult selectById(@RequestParam("id") Long id){
        Review review = reviewService.selectById(id);
        if (review!=null){
            return AjaxResult.success("操作成功",review);
        }else {
            return AjaxResult.error();
        }
    }

    @GetMapping("/getDataByPage")
    public AjaxResult selectByPage(@RequestParam("pageNum")int pageNum,@RequestParam("pageSize")int pageSize){
        PageBean<Review> reviewPageBean = reviewService.selectByPage(pageNum, pageSize);
        if (reviewPageBean!=null){
            return AjaxResult.success("查询成功",reviewPageBean);
        }else {
            return AjaxResult.error();
        }
    }





}